﻿using net8.Models;
using net8.Models.XiTong;
using net8.Views;
using net8.Views.XiTong;

namespace net8.Helper
{
    /// <summary>
    /// RedisCache公共帮助类
    /// </summary>
    public class RedisCacheHelper
    {
        public static List<MenuView> MenuList { get; set; }
        public static List<UserCache> UserList { get; set; }

        public static void Init()
        {
            MenuList = DbHelper.Db.Queryable<XtMenu>()
                //.Includes(r => r.PermissionList)
                .OrderBy(r => r.MenuOrder)
                .Select(r => new MenuView()
                {
                    Id = r.Id,
                    MenuName = r.MenuName,
                    MenuIcon = r.MenuIcon,
                    MenuOrder = r.MenuOrder,
                    MenuPath = r.MenuPath,
                    ParentId = r.ParentId,
                    //RoleList = r.PermissionList.Select(r => r.RoleId).ToList()
                }).ToList();

            UserList = DbHelper.Db.Queryable<XtUser>()
                .Includes(u => u.RoleList, ur => ur.Role, rm => rm.PermissionList)
                .Select(u => new UserCache()
                {
                    UserId = u.Id,
                    IsEnable = u.IsEnable,
                    LoginId = u.LoginId,
                    RoleName = u.RoleList.Select(ur => ur.Role.RoleName).ToList(),
                    MenuList = u.RoleList.SelectMany(ur => ur.Role.PermissionList.Select(rm => rm.MenuId)).ToList()
                }).ToList();
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="userId">当前用户id</param>
        /// <returns></returns>
        public static List<MenuView> GetMenuList(string userId)
        {
            UserCache user = UserList.First(u => u.UserId == Guid.Parse(userId));
            List<MenuView> temp = user.RoleName.Contains("超级管理员") ? MenuList : MenuList.Where(m => user.MenuList.Contains(m.Id)).ToList();

            return ToTreeMenu(temp, Guid.Empty);
        }

        /// <summary>
        /// 获取菜单下拉框
        /// </summary>
        /// <param name="userId">当前用户id</param>
        /// <returns></returns>
        public static List<TreeSelect> GetMenuSelect(string userId)
        {
            UserCache user = UserList.First(u => u.UserId == Guid.Parse(userId));

            List<MenuView> temp = user.RoleName.Contains("超级管理员") ? MenuList : MenuList.Where(m => user.MenuList.Contains(m.Id)).ToList();

            return ToTreeSelect(temp, Guid.Empty);
        }

        private static List<TreeSelect> ToTreeSelect(List<MenuView> list, Guid id)
        {
            List<TreeSelect> result = [];
            var temp = list.Where(t => t.ParentId == id).ToList();
            foreach (MenuView item in temp)
            {
                TreeSelect model = new()
                {
                    Value = item.Id,
                    Label = item.MenuName,
                    Children = ToTreeSelect(list, item.Id)
                };
                result.Add(model);
            }
            return result;
        }

        private static List<MenuView> ToTreeMenu(List<MenuView> list, Guid id)
        {
            List<MenuView> result = [];
            var temp = list.Where(t => t.ParentId == id).ToList();
            foreach (MenuView item in temp)
            {
                MenuView model = new()
                {
                    Id = item.Id,
                    MenuName = item.MenuName,
                    MenuIcon = item.MenuIcon,
                    MenuOrder = item.MenuOrder,
                    MenuPath = item.MenuPath,
                    ParentId = item.ParentId,
                    //RoleList = item.RoleList,
                    //Children = ToTreeMenu(list, item.Id).Count > 0 ? ToTreeMenu(list, item.Id) : default
                };

                var child = ToTreeMenu(list, item.Id);
                if (child.Count > 0)
                {
                    model.Children = child;
                }

                result.Add(model);
            }
            return result;
        }
    }

    public class UserCache
    {
        public Guid UserId { get; set; }
        public Guid LoginId { get; set; }
        public bool IsEnable { get; set; }
        public List<string> RoleName { get; set; }
        public List<Guid> MenuList { get; set; }
    }
}
